package com.service.imp;

import com.common.Base;
import com.common.Result;
import com.entity.User;
import com.repository.ArticleRepository;
import com.repository.FollowUserRepository;
import com.repository.UserRepository;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author：Luohuiyong
 * @Description:
 * @Date: Created in 11:06 2019/1/9
 */
@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private FollowUserRepository followUserRepository;

    @Override
    public User findOne(int id){
        Optional<User> optionalUser = userRepository.findById(id);
        User user = optionalUser.get();
        user.setArticles(articleRepository.countByUserId(id));
        user.setFollowers(followUserRepository.countByFollowedUser(id));
        return user;
    }

    /**
     * 新增一个实体
     * @param user
     * @return
     */
    @Transactional
    @Override
    public Result create(User user){
        user.setCreatedAt(new Date());
        userRepository.save(user);

        return Base.success();
    }

    /**
     * 登陆
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {
        User user1 = userRepository.findByUsername(user.getUsername());
        if(user1 == null){
            return Base.error("用户名不存在");
        }
        else{
            if(user1.getPassword().equals(user.getPassword())){
                return new Result<User>("200","登陆成功",user1);
            }
            else{
                return Base.error("密码错误");
            }
        }
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public Result register(User user) {
        User user1 = userRepository.findByUsername(user.getUsername());
        if (user1 != null){
            return Base.error("用户名已存在");
        }
        else{
            if(!StringUtils.hasText(user.getIdPhoto())){
                user.setIdPhoto(Base.defaultIdPhoto);
            }
            user.setCreatedAt(new Date());
            user.setIsRecommend(0);
            userRepository.save(user);

            return Base.success();

        }

    }

    /**
     * 查询推荐专题数据
     * @param
     * @return
     */
    @Override
    public Result recommend(User user){

        List<User> resList = userRepository.findByIsRecommend(1);
        for(int i = 0;i < resList.size();i++){
            resList.get(i).setArticles(articleRepository.countByUserId(resList.get(i).getId()));            //统计该作者共写了多少篇文章
            resList.get(i).setFollowers(followUserRepository.countByFollowedUser(resList.get(i).getId()));  //统计该作者的粉丝
        }

        return Base.success(resList);
    }


}
